﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package mochicrypt.as3 {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import flash.net.*;
    import flash.system.*;
    import flash.geom.*;

    public class MochiServices {

        private static var _container:Object;
        private static var _connected:Boolean = false;
        private static var _swfVersion:String;
        private static var _preserved:Object;
        public static var netupAttempted:Boolean = false;
        private static var _sendChannel:LocalConnection;
        public static var servicesSync:MochiSync = new MochiSync();
        private static var _clip:MovieClip;
        private static var _id:String;
        private static var _services:String = "services.swf";
        private static var _servURL:String = "http://www.mochiads.com/static/lib/services/";
        public static var widget:Boolean = false;
        private static var _timer:Timer;
        private static var _sendChannelName:String;
        private static var _loader:Loader;
        private static var _connecting:Boolean = false;
        private static var _mochiLocalConnection:MovieClip;
        private static var _listenChannelName:String = "__ms_";
        public static var onError:Object;
        public static var netup:Boolean = true;
        private static var _mochiLC:String = "MochiLC.swf";

        public static function isNetworkAvailable():Boolean{
            return (!((Security.sandboxType == "localWithFile")));
        }
        public static function get connected():Boolean{
            return (_connected);
        }
        private static function onReceive(_arg1:Object):void{
            var methodName:* = null;
            var pkg:* = _arg1;
            var cb:* = pkg.callbackID;
            var cblst:* = _clip._callbacks[cb];
            if (!cblst){
                return;
            };
            var method:* = cblst.callbackMethod;
            methodName = "";
            var obj:* = cblst.callbackObject;
            if (((obj) && ((typeof(method) == "string")))){
                methodName = method;
                if (obj[method] != null){
                    method = obj[method];
                } else {
                    trace((("Error: Method  " + method) + " does not exist."));
                };
            };
            if (method != undefined){
                try {
                    method.apply(obj, pkg.args);
                } catch(error:Error) {
                    trace(((("Error invoking callback method '" + methodName) + "': ") + error.toString()));
                };
            } else {
                if (obj != null){
                    try {
                        obj(pkg.args);
                    } catch(error:Error) {
                        trace(("Error invoking method on object: " + error.toString()));
                    };
                };
            };
            delete _clip._callbacks[cb];
        }
        public static function get childClip():Object{
            return (_clip);
        }
        public static function send(_arg1:String, _arg2:Object=null, _arg3:Object=null, _arg4:Object=null):void{
            if (_connected){
                _mochiLocalConnection.send(_sendChannelName, "onReceive", {
                    methodName:_arg1,
                    args:_arg2,
                    callbackID:_clip._nextcallbackID
                });
            } else {
                if ((((_clip == null)) || (!(_connecting)))){
                    trace(("Error: MochiServices not connected.   Please call MochiServices.connect().  Function: " + _arg1));
                    handleError(_arg2, _arg3, _arg4);
                    flush(true);
                    return;
                };
                _clip._queue.push({
                    methodName:_arg1,
                    args:_arg2,
                    callbackID:_clip._nextcallbackID
                });
            };
            if (_clip != null){
                if (((!((_clip._callbacks == null))) && (!((_clip._nextcallbackID == null))))){
                    _clip._callbacks[_clip._nextcallbackID] = {
                        callbackObject:_arg3,
                        callbackMethod:_arg4
                    };
                    _clip._nextcallbackID++;
                };
            };
        }
        private static function init(_arg1:String, _arg2:Object):void{
            _id = _arg1;
            if (_arg2 != null){
                _container = _arg2;
                loadCommunicator(_arg1, _container);
            };
        }
        private static function clickMovie(_arg1:String, _arg2:Function):MovieClip{
            var _local4:int;
            var _local14:Loader;
            var _local3:Array = [150, 21, 0, 7, 1, 0, 0, 0, 0, 98, 116, 110, 0, 7, 2, 0, 0, 0, 0, 116, 104, 105, 115, 0, 28, 150, 22, 0, 0, 99, 114, 101, 97, 116, 101, 69, 109, 112, 116, 121, 77, 111, 118, 105, 101, 67, 108, 105, 112, 0, 82, 135, 1, 0, 0, 23, 150, 13, 0, 4, 0, 0, 111, 110, 82, 101, 108, 101, 97, 115, 101, 0, 142, 8, 0, 0, 0, 0, 2, 42, 0, 114, 0, 150, 17, 0, 0, 32, 0, 7, 1, 0, 0, 0, 8, 0, 0, 115, 112, 108, 105, 116, 0, 82, 135, 1, 0, 1, 23, 150, 7, 0, 4, 1, 7, 0, 0, 0, 0, 78, 150, 8, 0, 0, 95, 98, 108, 97, 110, 107, 0, 154, 1, 0, 0, 150, 7, 0, 0, 99, 108, 105, 99, 107, 0, 150, 7, 0, 4, 1, 7, 1, 0, 0, 0, 78, 150, 27, 0, 7, 2, 0, 0, 0, 7, 0, 0, 0, 0, 0, 76, 111, 99, 97, 108, 67, 111, 110, 110, 101, 99, 116, 105, 111, 110, 0, 64, 150, 6, 0, 0, 115, 101, 110, 100, 0, 82, 79, 150, 15, 0, 4, 0, 0, 95, 97, 108, 112, 104, 97, 0, 7, 0, 0, 0, 0, 79, 150, 23, 0, 7, 0xFF, 0, 0xFF, 0, 7, 1, 0, 0, 0, 4, 0, 0, 98, 101, 103, 105, 110, 70, 105, 108, 108, 0, 82, 23, 150, 25, 0, 7, 0, 0, 0, 0, 7, 0, 0, 0, 0, 7, 2, 0, 0, 0, 4, 0, 0, 109, 111, 118, 101, 84, 111, 0, 82, 23, 150, 25, 0, 7, 100, 0, 0, 0, 7, 0, 0, 0, 0, 7, 2, 0, 0, 0, 4, 0, 0, 108, 105, 110, 101, 84, 111, 0, 82, 23, 150, 25, 0, 7, 100, 0, 0, 0, 7, 100, 0, 0, 0, 7, 2, 0, 0, 0, 4, 0, 0, 108, 105, 110, 101, 84, 111, 0, 82, 23, 150, 25, 0, 7, 0, 0, 0, 0, 7, 100, 0, 0, 0, 7, 2, 0, 0, 0, 4, 0, 0, 108, 105, 110, 101, 84, 111, 0, 82, 23, 150, 25, 0, 7, 0, 0, 0, 0, 7, 0, 0, 0, 0, 7, 2, 0, 0, 0, 4, 0, 0, 108, 105, 110, 101, 84, 111, 0, 82, 23, 150, 16, 0, 7, 0, 0, 0, 0, 4, 0, 0, 101, 110, 100, 70, 105, 108, 108, 0, 82, 23];
            var _local5:Array = [104, 0, 31, 64, 0, 7, 208, 0, 0, 12, 1, 0, 67, 2, 0xFF, 0xFF, 0xFF, 63, 3];
            var _local6:Array = [0, 64, 0, 0, 0];
            var _local7:MovieClip = new MovieClip();
            var _local8:LocalConnection = new LocalConnection();
            var _local9:String = ((("_click_" + Math.floor((Math.random() * 999999))) + "_") + Math.floor(new Date().time));
            _local8 = new LocalConnection();
            _local7.lc = _local8;
            _local7.click = _arg2;
            _local8.client = _local7;
            _local8.connect(_local9);
            var _local10:ByteArray = new ByteArray();
            var _local11:ByteArray = new ByteArray();
            _local11.endian = Endian.LITTLE_ENDIAN;
            _local11.writeShort(1);
            _local11.writeUTFBytes(((_arg1 + " ") + _local9));
            _local11.writeByte(0);
            var _local12:uint = ((_local3.length + _local11.length) + 4);
            var _local13:uint = (_local12 + 35);
            _local10.endian = Endian.LITTLE_ENDIAN;
            _local10.writeUTFBytes("FWS");
            _local10.writeByte(8);
            _local10.writeUnsignedInt(_local13);
            for each (_local4 in _local5) {
                _local10.writeByte(_local4);
            };
            _local10.writeUnsignedInt(_local12);
            _local10.writeByte(136);
            _local10.writeShort(_local11.length);
            _local10.writeBytes(_local11);
            for each (_local4 in _local3) {
                _local10.writeByte(_local4);
            };
            for each (_local4 in _local6) {
                _local10.writeByte(_local4);
            };
            _local14 = new Loader();
            _local14.loadBytes(_local10);
            _local7.addChild(_local14);
            return (_local7);
        }
        public static function stayOnTop():void{
            _container.addEventListener(Event.ENTER_FRAME, MochiServices.bringToTop, false, 0, true);
            if (_clip != null){
                _clip.visible = true;
            };
        }
        public static function addLinkEvent(_arg1:String, _arg2:String, _arg3:DisplayObjectContainer, _arg4:Function=null):void{
            var avm1Click:* = null;
            var x:* = null;
            var req:* = null;
            var loader:* = null;
            var setURL:* = null;
            var err:* = null;
            var complete:* = null;
            var url:* = _arg1;
            var burl:* = _arg2;
            var btn:* = _arg3;
            var onClick = _arg4;
            var vars:* = new Object();
            vars["mav"] = getVersion();
            vars["swfv"] = "9";
            vars["swfurl"] = btn.loaderInfo.loaderURL;
            vars["fv"] = Capabilities.version;
            vars["os"] = Capabilities.os;
            vars["lang"] = Capabilities.language;
            vars["scres"] = ((Capabilities.screenResolutionX + "x") + Capabilities.screenResolutionY);
            var s:* = "?";
            var i:* = 0;
            for (x in vars) {
                if (i != 0){
                    s = (s + "&");
                };
                i = (i + 1);
                s = (((s + x) + "=") + escape(vars[x]));
            };
            req = new URLRequest("http://x.mochiads.com/linkping.swf");
            loader = new Loader();
            setURL = function (_arg1:String):void{
                if (avm1Click){
                    btn.removeChild(avm1Click);
                };
                avm1Click = clickMovie(_arg1, onClick);
                var _local2:Rectangle = btn.getBounds(btn);
                btn.addChild(avm1Click);
                avm1Click.x = _local2.x;
                avm1Click.y = _local2.y;
                avm1Click.scaleX = (0.01 * _local2.width);
                avm1Click.scaleY = (0.01 * _local2.height);
            };
            err = function (_arg1:Object):void{
                netup = false;
                _arg1.target.removeEventListener(_arg1.type, arguments.callee);
                setURL(burl);
            };
            complete = function (_arg1:Object):void{
                _arg1.target.removeEventListener(_arg1.type, arguments.callee);
            };
            if (netup){
                setURL((url + s));
            } else {
                setURL(burl);
            };
            if (!((netupAttempted) || (_connected))){
                netupAttempted = true;
                loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, err);
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
                loader.load(req);
            };
        }
        public static function warnID(_arg1:String, _arg2:Boolean):void{
            _arg1 = _arg1.toLowerCase();
            if (_arg1.length != 16){
                trace((("WARNING: " + (_arg2) ? "board" : "game") + " ID is not the appropriate length"));
                return;
            };
            if (_arg1 == "1e113c7239048b3f"){
                if (_arg2){
                    trace("WARNING: Using testing board ID");
                } else {
                    trace("WARNING: Using testing board ID as game ID");
                };
                return;
            };
            if (_arg1 == "84993a1de4031cd8"){
                if (_arg2){
                    trace("WARNING: Using testing game ID as board ID");
                } else {
                    trace("WARNING: Using testing game ID");
                };
                return;
            };
            var _local3:Number = 0;
            while (_local3 < _arg1.length) {
                switch (_arg1.charAt(_local3)){
                    case "0":
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                    case "a":
                    case "b":
                    case "c":
                    case "d":
                    case "e":
                    case "f":
                        break;
                    default:
                        trace(("WARNING: Board ID contains illegal characters: " + _arg1));
                        return;
                };
                _local3++;
            };
        }
        public static function disconnect():void{
            if (((_connected) || (_connecting))){
                if (_clip != null){
                    if (_clip.parent != null){
                        if ((_clip.parent is Sprite)){
                            Sprite(_clip.parent).removeChild(_clip);
                            _clip = null;
                        };
                    };
                };
                _connecting = (_connected = false);
                flush(true);
                try {
                    _mochiLocalConnection.close();
                } catch(error:Error) {
                };
            };
            if (_timer != null){
                try {
                    _timer.stop();
                } catch(error:Error) {
                };
            };
        }
        public static function allowDomains(_arg1:String):String{
            var _local2:String;
            if (Security.sandboxType != "application"){
                Security.allowDomain("*");
                Security.allowInsecureDomain("*");
            };
            if (_arg1.indexOf("http://") != -1){
                _local2 = _arg1.split("/")[2].split(":")[0];
                if (Security.sandboxType != "application"){
                    Security.allowDomain(_local2);
                    Security.allowInsecureDomain(_local2);
                };
            };
            return (_local2);
        }
        public static function getVersion():String{
            return ("3.4c as3");
        }
        public static function doClose():void{
            _container.removeEventListener(Event.ENTER_FRAME, MochiServices.bringToTop);
        }
        private static function flush(_arg1:Boolean):void{
            var _local2:Object;
            var _local3:Object;
            if (((_clip) && (_clip._queue))){
                while (_clip._queue.length > 0) {
                    _local2 = _clip._queue.shift();
                    _local3 = null;
                    if (_local2 != null){
                        if (_local2.callbackID != null){
                            _local3 = _clip._callbacks[_local2.callbackID];
                        };
                        delete _clip._callbacks[_local2.callbackID];
                        if (((_arg1) && (!((_local3 == null))))){
                            handleError(_local2.args, _local3.callbackObject, _local3.callbackMethod);
                        };
                    };
                };
            };
        }
        public static function get id():String{
            return (_id);
        }
        private static function onEvent(_arg1:Object):void{
            var _local2:String = _arg1.target;
            var _local3:String = _arg1.event;
            switch (_local2){
                case "events":
                    MochiEvents.triggerEvent(_arg1.event, _arg1.args);
                    break;
                case "coins":
                    MochiCoins.triggerEvent(_arg1.event, _arg1.args);
                    break;
                case "sync":
                    servicesSync.triggerEvent(_arg1.event, _arg1.args);
                    break;
            };
        }
        private static function urlOptions(_arg1:Object):Object{
            var _local3:String;
            var _local4:Array;
            var _local5:Number;
            var _local6:Array;
            var _local2:Object = {};
            if (_arg1.stage){
                _local3 = _arg1.stage.loaderInfo.parameters.mochiad_options;
            } else {
                _local3 = _arg1.loaderInfo.parameters.mochiad_options;
            };
            if (_local3){
                _local4 = _local3.split("&");
                _local5 = 0;
                while (_local5 < _local4.length) {
                    _local6 = _local4[_local5].split("=");
                    _local2[unescape(_local6[0])] = unescape(_local6[1]);
                    _local5++;
                };
            };
            return (_local2);
        }
        public static function setContainer(_arg1:Object=null, _arg2:Boolean=true):void{
            if (_arg1 != null){
                if ((_arg1 is Sprite)){
                    _container = _arg1;
                };
            };
            if (_arg2){
                if ((_container is Sprite)){
                    Sprite(_container).addChild(_clip);
                };
            };
        }
        private static function handleError(_arg1:Object, _arg2:Object, _arg3:Object):void{
            var args:* = _arg1;
            var callbackObject:* = _arg2;
            var callbackMethod:* = _arg3;
            if (args != null){
                if (args.onError != null){
                    args.onError.apply(null, ["NotConnected"]);
                };
                if (((!((args.options == null))) && (!((args.options.onError == null))))){
                    args.options.onError.apply(null, ["NotConnected"]);
                };
            };
            if (callbackMethod != null){
                args = {};
                args.error = true;
                args.errorCode = "NotConnected";
                if (((!((callbackObject == null))) && ((callbackMethod is String)))){
                    try {
                        var _local5 = callbackObject;
                        _local5[callbackMethod](args);
                    } catch(error:Error) {
                    };
                } else {
                    if (callbackMethod != null){
                        try {
                            callbackMethod.apply(args);
                        } catch(error:Error) {
                        };
                    };
                };
            };
        }
        private static function loadError(_arg1:Object):void{
            _clip._mochiad_ctr_failed = true;
            trace("MochiServices could not load.");
            MochiServices.disconnect();
            MochiServices.onError("IOError");
        }
        private static function initComChannels():void{
            if (!_connected){
                trace("[SERVICES_API] connected!");
                _connecting = false;
                _connected = true;
                _mochiLocalConnection.send(_sendChannelName, "onReceive", {methodName:"handshakeDone"});
                _mochiLocalConnection.send(_sendChannelName, "onReceive", {
                    methodName:"registerGame",
                    preserved:_preserved,
                    id:_id,
                    clip:_container,
                    version:getVersion(),
                    parentURL:_container.loaderInfo.loaderURL
                });
                _clip.onReceive = onReceive;
                _clip.onEvent = onEvent;
                _clip.onError = function ():void{
                    MochiServices.onError("IOError");
                };
                while (_clip._queue.length > 0) {
                    _mochiLocalConnection.send(_sendChannelName, "onReceive", _clip._queue.shift());
                };
            };
        }
        private static function loadLCBridge(_arg1:Object):void{
            var loader:* = null;
            var clip:* = _arg1;
            loader = new Loader();
            var mochiLCURL:* = (_servURL + _mochiLC);
            var req:* = new URLRequest(mochiLCURL);
            var complete:* = function (_arg1:Object):void{
                _mochiLocalConnection = MovieClip(loader.content);
                listen();
            };
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
            loader.load(req);
            clip.addChild(loader);
        }
        private static function listen():void{
            _mochiLocalConnection.connect(_listenChannelName);
            _clip.handshake = function (_arg1:Object):void{
                MochiServices.comChannelName = _arg1.newChannel;
            };
            trace("Waiting for MochiAds services to connect...");
        }
        public static function get clip():Object{
            return (_container);
        }
        public static function set comChannelName(_arg1:String):void{
            if (_arg1 != null){
                if (_arg1.length > 3){
                    _sendChannelName = (_arg1 + "_fromgame");
                    initComChannels();
                };
            };
        }
        private static function loadCommunicator(_arg1:String, _arg2:Object):MovieClip{
            var _local3:String = ("_mochiservices_com_" + _arg1);
            if (_clip != null){
                return (_clip);
            };
            if (!MochiServices.isNetworkAvailable()){
                return (null);
            };
            if (urlOptions(_arg2).servURL){
                _servURL = urlOptions(_arg2).servURL;
            };
            var _local4:String = (_servURL + _services);
            if (urlOptions(_arg2).servicesURL){
                _local4 = urlOptions(_arg2).servicesURL;
            };
            _listenChannelName = (_listenChannelName + ((Math.floor(new Date().time) + "_") + Math.floor((Math.random() * 99999))));
            MochiServices.allowDomains(_local4);
            _clip = createEmptyMovieClip(_arg2, _local3, 10336, false);
            loadLCBridge(_clip);
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
            var _local5:URLRequest = new URLRequest(_local4);
            var _local6:URLVariables = new URLVariables();
            _local6.listenLC = _listenChannelName;
            _local6.mochiad_options = _arg2.loaderInfo.parameters.mochiad_options;
            _local6.api_version = getVersion();
            if (widget){
                _local6.widget = true;
            };
            _local5.data = _local6;
            _loader.load(_local5);
            _clip.addChild(_loader);
            _clip._mochiservices_com = _loader;
            _sendChannel = new LocalConnection();
            _clip._queue = [];
            _clip._nextcallbackID = 0;
            _clip._callbacks = {};
            _timer = new Timer(10000, 1);
            _timer.addEventListener(TimerEvent.TIMER, connectWait);
            _timer.start();
            return (_clip);
        }
        public static function connect(_arg1:String, _arg2:Object, _arg3:Object=null):void{
            var id:* = _arg1;
            var clip:* = _arg2;
            var onError = _arg3;
            warnID(id, false);
            if ((clip is DisplayObject)){
                if (clip.stage == null){
                    trace("MochiServices connect requires the containing clip be attached to the stage");
                };
                if (((!(_connected)) && ((_clip == null)))){
                    trace("MochiServices Connecting...");
                    _connecting = true;
                    init(id, clip);
                };
            } else {
                trace("Error, MochiServices requires a Sprite, Movieclip or instance of the stage.");
            };
            if (onError != null){
                MochiServices.onError = onError;
            } else {
                if (MochiServices.onError == null){
                    MochiServices.onError = function (_arg1:String):void{
                        trace(_arg1);
                    };
                };
            };
        }
        public static function createEmptyMovieClip(_arg1:Object, _arg2:String, _arg3:Number, _arg4:Boolean=true):MovieClip{
            var parent:* = _arg1;
            var name:* = _arg2;
            var depth:* = _arg3;
            var doAdd:Boolean = _arg4;
            var mc:* = new MovieClip();
            if (doAdd){
                if (((false) && (depth))){
                    parent.addChildAt(mc, depth);
                } else {
                    parent.addChild(mc);
                };
            };
            try {
                parent[name] = mc;
            } catch(e:Error) {
                throw (new Error("MochiServices requires a clip that is an instance of a dynamic class.  If your class extends Sprite or MovieClip, you must make it dynamic."));
            };
            mc["_name"] = name;
            return (mc);
        }
        public static function bringToTop(_arg1:Event=null):void{
            var e = _arg1;
            if (((!((MochiServices.clip == null))) && (!((MochiServices.childClip == null))))){
                try {
                    if (MochiServices.clip.numChildren > 1){
                        MochiServices.clip.setChildIndex(MochiServices.childClip, (MochiServices.clip.numChildren - 1));
                    };
                } catch(errorObject:Error) {
                    trace("Warning: Depth sort error.");
                    _container.removeEventListener(Event.ENTER_FRAME, MochiServices.bringToTop);
                };
            };
        }
        public static function connectWait(_arg1:TimerEvent):void{
            if (!_connected){
                _clip._mochiad_ctr_failed = true;
                trace("MochiServices could not load. (timeout)");
                MochiServices.disconnect();
                MochiServices.onError("IOError");
            };
        }

    }
}//package mochicrypt.as3 
